﻿using Abp.Authorization;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Commodity.Commodities;
using shenghua.Commodity.Commodities.Dto;
using shenghua.Common.Dto;
using shenghua.Member.ShoppingCart.Dto;
using shenghua.Order.Orders.Median;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.ShoppingCart
{
    [AbpAuthorize(PermissionNames.Member)]
    public class ShoppingCartAppService: shenghuaAppServiceBase, IShoppingCartAppService
    {
        private readonly ShoppingCartManager _shoppingCartManager;
        private readonly CommodityManager _commodityManager;

        public ShoppingCartAppService(
            ShoppingCartManager shoppingCartManager,
            CommodityManager commodityManager
            )
        {
            _shoppingCartManager = shoppingCartManager;
            _commodityManager = commodityManager;
        }

        public async Task<ShoppingCartCommodityDto> AddCommodityToShoppingCart(AddCommodityToShoppingCartDto input)
        {
            ShoppingCartCommodity shoppingCartCommodity = new ShoppingCartCommodity
            {
                CommodityId = input.CommodityId,
                Count = input.Count,
                ShoppingOptionId = input.ShoppingOptionId
            };
            Member.Members.Member member = await GetCurrentMemberAsync();
            var item = await _shoppingCartManager.AddCommodityToShoppingCart(member, shoppingCartCommodity);
            var commodityCollectionItem = await _commodityManager.GetCommodityCollectionItem(member, item.Count, item.CommodityId, item.ShoppingOptionId);
            return new ShoppingCartCommodityDto(item, commodityCollectionItem);
        }

        public async Task<List<ShoppingCartCommodityDto>> GetAll()
        {
            Member.Members.Member member = await GetCurrentMemberAsync();
            var items = await _shoppingCartManager.GetAll(member);

            List<ShoppingCartCommodityDto> shoppingCartCommodityDtos = new List<ShoppingCartCommodityDto>();
            foreach (var item in items)
            {
                CommodityCollectionItem commodityCollectionItem = null;
                try
                {
                    commodityCollectionItem = await _commodityManager.GetCommodityCollectionItem(member, item.Count, item.CommodityId, item.ShoppingOptionId);
                }
                catch { }

                if(commodityCollectionItem != null)
                {
                    shoppingCartCommodityDtos.Add(new ShoppingCartCommodityDto(item, commodityCollectionItem));
                }
            }
            return shoppingCartCommodityDtos;
        }

        public async Task Clear()
        {
            await _shoppingCartManager.ClearShoppingCart((int)AbpSession.UserId);
        }

        [HttpPost]
        public async Task RemoveCommodityFromShoppingCart(DeleteDto input)
        {
            foreach(int id in input.Ids)
            {
                await _shoppingCartManager.RemoveCommodityFromShoppingCart((int)AbpSession.UserId, id);
            }
        }
    }
}
